<template>
  <el-menu
    active-text-color="rgb(64, 158, 255)"
    background-color="#304156"
    class="el-menu-vertical-demo"
    text-color="#fff"
    :collapse="isCollapse"
    router
    unique-opened
    :default-active="defalutUrl"
  >
    <el-menu-item index="/index">
      <i class="iconfont homepage"></i>
      <span>首页</span>
    </el-menu-item>

    <el-sub-menu :index="`/${p.name}`" v-for="p in menus" :key="p.id">
      <template #title>
        <i class="iconfont" :class="p.icon"></i>
        <span>{{ p.title }}</span>
      </template>
      <template v-for="sub in p.children" :key="sub.id">
        <el-menu-item :index="`/${p.name}/${sub.name}`" v-if="sub.hidden == 0">
          <i class="iconfont" :class="sub.icon"></i>
          {{ sub.title }}
        </el-menu-item>
      </template>
    </el-sub-menu>
  </el-menu>
</template>

<script setup lang="ts">
import { ref, watchEffect } from "vue";
import { useRoute } from "vue-router";
import { inject } from "vue";
import { useUserStore } from "@/store/user";

const userStore = useUserStore();
// 控制菜单显示隐藏
const isCollapse = inject<boolean>("isCollapse");
const menus = userStore.routes;
// 控制菜单高亮
const defalutUrl = ref("/index");
const router = useRoute();
watchEffect(() => {
  defalutUrl.value = router.path;
});
</script>

<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 250px;
}
</style>
